package com.Rest2Go;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

public class MoveEatGallery extends Gallery
{
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
	{
		if (mIsBeingUpdated != true)
		{
				mIsBeingUpdated=true;
			
				mControledGallery.onFling(e1, e2, velocityX, velocityY);
		}else{
			mIsBeingUpdated=false;
		}
		return super.onFling(e1, e2, velocityX, velocityY);
	}

	private boolean	scrollingHorizontally	= true;
	private boolean	mIsBeingUpdated			= false;

	@Override
	protected void onAnimationStart()
	{
		mControledGallery.setSelection(this.getSelectedItemPosition());
		super.onAnimationStart();
	}

	MoveEatGallery	mControledGallery;
	String			attributeNamespace	= "http://schemas.android.com/apk/res/com.Rest2Go.MoveEatGallery";

	public MoveEatGallery(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MoveEatGallery(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public MoveEatGallery(Context context)
	{
		super(context);
		// TODO Auto-generated constructor stub
	}

	public void setControlledGallery(MoveEatGallery gallery)
	{
		mControledGallery = gallery;
	}

	public MoveEatGallery(Context context, MoveEatGallery controledGallery)
	{
		super(context);
		mControledGallery = controledGallery;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev)
	{
		super.onInterceptTouchEvent(ev);
		return scrollingHorizontally;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
	{
		scrollingHorizontally = true;
		return super.onScroll(e1, e2, distanceX, distanceY);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		switch (event.getAction())
		{
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				scrollingHorizontally = false;
		}

		return super.onTouchEvent(event);
	}

	public boolean isBeingUpdated()
	{
		return mIsBeingUpdated;
	}

	public void setBeingUpdated(boolean isBeingUpdated)
	{
		mIsBeingUpdated = isBeingUpdated;
	}

}
